package com.frinder.data.base;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Sort;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName PageWrapper
 * @Author frinder
 * @Description 分页参数组合类
 * @Date 2019/1/19 15:20
 * @Version 1.0
 */
@Data
public class PageWrapper<T> {

    public PageWrapper() {
        orders.add(Order.builder().field("id").strategy(OrderStrategy.DESC).build());
    }

    private T target;
    private int page = 1;
    private int size = 10;
    private Date createTimeFrom;
    private Date createTimeTo;
    private Date updateTimeFrom;
    private Date updateTimeTo;
    @Builder.Default
    private List<String> equalFields = Lists.newArrayList();
    @Builder.Default
    private List<String> notEqualFields = Lists.newArrayList();
    /**
     * 使用 inFields & notInFields 时，
     * 即使前端 target 不需要其它属性，也务必使用“ "target":{} ”的方式，初始化 target，否则查询失效
     */
    @Builder.Default
    private Map<String, List<Object>> inFields = Maps.newHashMap();
    @Builder.Default
    private Map<String, List<Object>> notInFields = Maps.newHashMap();
    /** 排序列表 */
    @Builder.Default
    private List<Order> orders = Lists.newArrayList();
    /** 大于等于 & 小于等于 */
    @Builder.Default
    private Map<String, Integer> greaterThanFields = Maps.newHashMap();
    @Builder.Default
    private Map<String, Integer> greaterThanOrEqualFields = Maps.newHashMap();
    @Builder.Default
    private Map<String, Integer> lessThanFields = Maps.newHashMap();
    @Builder.Default
    private Map<String, Integer> lessThanOrEqualFields = Maps.newHashMap();


    public enum OrderStrategy {
        DESC {
            @Override
            public Sort.Direction get() {
                return Sort.Direction.DESC;
            }
        },
        ASC {
            @Override
            public Sort.Direction get() {
                return Sort.Direction.ASC;
            }
        },
        ;

        abstract Sort.Direction get();
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Order {
        private String field;
        private OrderStrategy strategy;
    }

}
